package com.example.springtrans.controller;

import com.example.springtrans.service.LogService;
import com.example.springtrans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/user")
@Slf4j

public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private LogService logService;
    @Transactional( propagation = Propagation.REQUIRED)
    @RequestMapping("/registry")
    public Boolean registry(String userName, String password) throws IOException {
        //参数校验
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        //注册
        Integer userResult = userService.insertUser(userName, password);
        log.info("数据插入 user_info:{}", userResult);
        Integer logResult = logService.insertLog(userName, "用户注册");
        log.info("数据插入 log_info: {}", logResult);
        return true;
    }
}
